<template>
  <div class="share-container">
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <span
            :class="['mui-control-item',item.id===0? 'mui-active': '']"
            v-for="item in category"
            :key="item.id"
            @tap="handleCateItemClick(item.id)"
          >{{item.title}}</span>
        </div>
      </div>
    </div>

    <ul class="lazyul">
      <router-link tag='li' v-for="item in photolist" :key="item.id" :to="'/home/photoinfo/'+item.id">
        <img v-lazy="item.img_url" />
        <div class="info">
          <div class="info-title">{{ item.title }}</div>
          <div class="info-content">{{ item.content }}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
import mui from "../../lib/mui/js/mui.min.js";
export default {
  created() {
    // 获取分类数据
    this.__getCategory();
    // 获取全部数据
    this.__getPhotoByCategory(this.cateId);
  },
  mounted() {
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  },
  data() {
    return {
      category: [], // 分类列表数据
      cateId: 0, // 分类数据的ID  全部 = 0
      photolist: [] // 图片列表数据
    };
  },
  methods: {
    // ============ 发送网络请求 =======================
    async __getCategory() {
      const { data: res } = await this.$http.get("getimgcategory");
      if (res.status !== 0) return;
      res.message.unshift({ id: 0, title: "全部" });
      this.category = res.message;
    },
    async __getPhotoByCategory(cateId) {
      const { data: res } = await this.$http.get(`getimages/${cateId}`);
      if (res.status !== 0) return;
      this.photolist = res.message;
    },
    // ========== 事件监听 =====================
    handleCateItemClick(cateId) {
      this.__getPhotoByCategory(cateId);
    }
  }
};
</script>
<style lang='scss' scoped>
.mui-slider {
  touch-action: pan-x;
}

.lazyul {
  list-style: none;
  margin: 0;
  padding: 10px;
  li {
    position: relative;
    box-shadow: 0 0 7px gray;
    & + li {
      margin-top: 10px;
    }
    text-align: center;
    background-color: #ccc;
    img {
      width: 100%;
      vertical-align: middle;
    }
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }
    .info {
      position: absolute;
      bottom: 0;
      color: #fff;
      text-align: left;
      max-height: 86px;
      background-color: rgba(0,0,0,.5);
      .info-title {
        font-size: 14px;
      }
      .info-content {
        font-size: 13px;
        text-indent: 2em;
      }
    }
  }
}
</style>